#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc_c.h>

using namespace cv;

JNIEXPORT void JNICALL Java_hieuph_facerecognition_FaceDetect_FaceDetect_convertRGBToGray(
		JNIEnv *jenv, jclass, jint width, jint height,
		jintArray mPhotoIntArray) {

	// get the data pointer
	jint * in = jenv->GetIntArrayElements(mPhotoIntArray, 0);

	// Build the Mat structure for input data
	Mat mSrc(height, width, CV_8UC4, (unsigned char *) in);

	//Convert Mat to IplImage
	IplImage mSrcImg = mSrc;

	//Create the gray image for input data.
	IplImage * mSrcGrayImg = cvCreateImage(cvGetSize(&mSrcImg), mSrcImg.depth,
			1);

	//Convert to Gray image
	cvCvtColor(&mSrcImg, mSrcGrayImg, CV_BGR2GRAY);

	// Release the pointer
	jenv->ReleaseIntArrayElements(mPhotoIntArray, in, 0);
}

